package com.zhouheng.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


/**
 * Master数据源配置
 *
 * @MapperScan 扫描Mapper 接口并容器管理
 * @Primary 多个DataSource，首先使用该多个DataSource（有且仅有一个，不然报错）
 * @ConditionalOnMissingBean 在实例化的时候，按照bean的类型进行检测
 * @author 周恒
 * @date 20180830 15:41:53
 * @since v1.0
 */
@Configuration
@MapperScan(basePackages = "com.zhouheng.module.multipledatasource.master.**.dao", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {

    private MasterDataSourceProperty masterDataSourceProperty;

    public MasterDataSourceConfig(MasterDataSourceProperty masterDataSourceProperty) {
        this.masterDataSourceProperty = masterDataSourceProperty;
    }

    @Bean(name = "masterDataSource")
    @Primary
    @ConditionalOnMissingBean(DruidDataSource.class)
    public DataSource masterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(masterDataSourceProperty.getDriverClassName());
        dataSource.setUrl(masterDataSourceProperty.getUrl());
        dataSource.setUsername(masterDataSourceProperty.getUserName());
        dataSource.setPassword(masterDataSourceProperty.getPassWord());
        return dataSource;
    }

    @Bean(name = "masterTransactionManager")
    @Primary
    public DataSourceTransactionManager masterTransactionManager() {
        return new DataSourceTransactionManager(masterDataSource());
    }

    @Bean(name = "masterSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/multipledatasource/master/*.xml"));
        return sessionFactory.getObject();
    }

    @Bean(name = "masterTransactionManager")
    @Primary
    public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name= "masterSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
